angular.module('email-bind', [])

.controller 'EmailBindCtrl', ($scope,userData, $state, $http, $ionicHistory,showToast) ->
    _.extend $scope,
        info:
          email: undefined
        goBack: ->
          $ionicHistory.goBack()
        checkMail: ->
            reg=/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/
            reg.test($scope.info.email)
        doSave: ->
            if !$scope.checkMail()
                showToast '请输入正确的邮箱格式'
                return
            if $scope.oriEmail==$scope.info.email #未修改邮箱,直接返回
                $ionicHistory.goBack()
                return
            postData={
                action:'bindemail'
                userid:userData.getToken()
                email:$scope.info.email
            }
            $http.post('/seller/',postData)
            .success (resp) ->
                console.log resp
                if resp.status=='0'
                    showToast '绑定邮箱成功'
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error (err) ->
                showToast '绑定邮箱失败'
        clear: () ->
          $scope.info.email = ''
    loadEmail=()->
        requestData={
            action:'getbindemail'
            userid:userData.getToken()
        }
        $http.get('/seller',params:requestData)
        .success (resp) ->
            if resp.status=='0'
                $scope.oriEmail=resp.email
                $scope.info.email=resp.email
            else
                showToast resp.msg
        .error (err) ->
            showToast '获取邮箱失败'
    $scope.$on '$ionicView.enter', ->
        loadEmail()


